package mousecollide;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;
import java.util.Random;

import chooser.ChooseMenu;
import chooser.GameFrame;

public class GameStart extends GameFrame implements Runnable
{
	private static final long serialVersionUID = 5642619743815256222L;

	private Thread game;
	private boolean isRunning = false;
	private int width, height;
	private BufferStrategy myStrategy;
	
	private final int FPS = 50;
	private int sleepTime;
	
	private Sprite[] objList;
	private final int robotNum = 50;
	private Random random;
	
	/**
	 *  Player, only a square currently
	 */
	private Sprite player;

	public GameStart(int i)
	{
		if (i != 0)
		{
			showon();
		}
	}
	
	private void shutDown()
	{
		setIgnoreRepaint(false);
		isRunning = false;
		this.dispose();
		game = null;
		ChooseMenu.getInstance().setVisible(true);
	}
	
	public void showon()
	{
		setUndecorated(true);
		setResizable(false);
		setIgnoreRepaint(true);

		/*
		 * Init FSEM Mode & Create Buffer for Page Flipping
		 */
		GraphicsDevice myDevice;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		myDevice = ge.getDefaultScreenDevice();
		if (!myDevice.isFullScreenSupported())
		{
			System.out.println("Full-screen exclusive mode not supported");
			shutDown();
		}
		try
		{
			myDevice.setFullScreenWindow(this);
			createBufferStrategy(2);
		}
		catch (Exception e)
		{
			System.out.println("Error while creating buffer strategy");
			shutDown();
		}
		
		setVisible(true);
		
		width = getBounds().width;
		height = getBounds().height;
		
		/*
		 * Create Objects, set their Position and action frame.
		 */
		player = new Player(6, 3);
		player.setPosition(200, 300);
		player.setFrame(width, height);
		random = new Random();
		objList = new Sprite[robotNum];
		objList[0] = player;
		
		for (int i = 1; i < objList.length; i++)
		{
			objList[i] = new Robot(6, 2);
			objList[i].setPosition(random.nextInt(width-12)+6, random.nextInt(height-12)+6);
			objList[i].setFrame(width, height);
		}


		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e)
			{
				player.setDestination(e.getX(), e.getY());
			}
		});
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
				{
					isRunning = false;
					//System.exit(0);
					shutDown();
				}
			}
		});
		
		startgame();
	}

	public void startgame()
	{
		if (game == null || !isRunning)
		{
			game = new Thread(this);
			game.start();
		}
	}

	public void run()
	{
		isRunning = true;
		sleepTime = 1000/FPS;

		while (isRunning)
		{
			gameUpdate();
			myStrategy = getBufferStrategy();
			Graphics graphics = myStrategy.getDrawGraphics();
			try
			{
				graphicUpdate(graphics);
			}
			finally
			{
				graphics.dispose();
			}
			myStrategy.show();
			
			try
			{
				Thread.sleep(sleepTime);
			}
			catch (InterruptedException ie)
			{
				
			}
		}
		myStrategy.dispose();
	}

	private void gameUpdate()
	{
		if (isRunning)
		{
			/*
			 * Collision detection, not efficiency enough.
			 * 
			 */
			for (int i = 0;  i< objList.length-1; i++)
			{
				for (int j = i+1; j < objList.length; j++)
				{
					if (objList[i].collideWith(objList[j]))
					{
						objList[i].bounce(objList[j]);
						objList[j].bounce(objList[i]);
					}
				}
			}
			for (int i = 0;  i< objList.length; i++)
			{
				objList[i].move();
			}
		}
	}

	private void graphicUpdate(Graphics g)
	{
		g.setColor(Color.BLACK);
	    g.fillRect (0, 0, width, height);
	    for (int i = 0;  i< objList.length; i++)
		{
			objList[i].draw(g);
		}
	}
}
